﻿using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lamex.SystemManager.Model;
using Lamex.SystemManager.BLL;
using Lamex.Base;
using DevExpress.XtraEditors;
using Lamex.Common;
namespace Lamex.SystemManager.UI
{
    public partial class frmMenuEdit : frmShow
    {
        public string _ID = "";
        Sys_MenuInfo info = null;
        public frmMenuEdit(string ID)
        {
            _ID=ID;
            InitializeComponent();
            if (!string.IsNullOrEmpty(ID))
            {
                info = BaseFactory<Sys_Menu>.Instance.GetEntriyList(" 1=1 and ID='"+ID+"'").FirstOrDefault();
                
                txtName.Text=info.Name;
                txtMenu.Value=info.PID;
                txtSystemType.Text=info.WinformType;
                txtSortCode.Text=info.SortCode;
                txtWinformId.Text=info.WinformId;
                txtWinformType.Text=info.WinformType;
                txtIcon.Text=info.Image;
                chkEnabled.Checked=info.Enabled;
                txtEngName.Text = info.EngName;
            }
            else
            Clear();
            txtSystemType.Text = "001";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                XtraMessageBox.Show("【菜单名称不能为空】！");
                return;
            }
            if (string.IsNullOrEmpty(txtWinformId.Text))
            {
                XtraMessageBox.Show("【功能ID不能为空】！");
                return;
            }
            if (string.IsNullOrEmpty(txtWinformType.Text))
            {
                XtraMessageBox.Show("【窗体类型不能为空】！");
                return;
            }
            
           
            
            if(string.IsNullOrEmpty(_ID))
            {
                Sys_MenuInfo model = BaseFactory<Sys_Menu>.Instance.GetEntriyList(" 1=1 and WinformId='" + txtWinformId.Text + "'").FirstOrDefault();
                Sys_FunctionInfo function = new Sys_FunctionInfo(); 
                if (model!=null)
                {
                    XtraMessageBox.Show("【功能ID已存在，请从新命名窗体名称】！");
                    return;
                }
                Sys_MenuInfo newinfo = new Sys_MenuInfo();
                newinfo.PID = txtMenu.Value;
                newinfo.Name = txtName.Text;
                newinfo.WinformId = txtWinformId.Text;
                newinfo.SortCode = txtSortCode.Text;
                newinfo.WinformType = txtWinformType.Text;
                newinfo.Image = txtIcon.Text;
                newinfo.SystemType = txtSystemType.Text;
                newinfo.Enabled = chkEnabled.Checked;
                if (txtMenu.Value == "-1")
                    newinfo.MainMenu = txtWinformType.Text;
                newinfo.CreateBy = Loginer.CurrentUser.Account;
                newinfo.CreateTime = DateTime.Now;
                newinfo.EngName = txtEngName.Text;
                if (BaseFactory<Sys_Menu>.Instance.Insert(newinfo))
                {
                    //if (txtMenu.Value == "-1")
                    //{
                    Sys_MenuInfo fun = BaseFactory<Sys_Menu>.Instance.GetEntriyList(" 1=1 and PID='" + txtMenu.Value + "' and WinformId='" + txtWinformId.Text + "'").FirstOrDefault();
                    if (fun != null)
                    {
                        //function.ID = fun.ID;
                        //function.PID = txtMenu.Value;
                        //function.FunName = txtName.Text;
                        //function.FunControlID = txtWinformId.Text;
                        //function.SystemType = txtSystemType.Text;
                        //function.SortCode = txtSortCode.Text;
                        //function.CreateBy = "lzw";
                        //function.CreateDate = DateTime.Now;
                        string sql = @"INSERT INTO dbo.Sys_Function( ID ,PID ,FunName , FunControlID ,SystemType ,SortCode ,IsVaild ,CreateBy , CreateDate,SysMenu )
                                   VALUES('" + fun.ID + "','" + fun.PID + "','" + fun.Name + "','" + fun.WinformId + "','" + fun.SystemType + "','" + fun.SortCode + "','False','" + Loginer.CurrentUser.Account + "','" + DateTime.Now + "','" + fun.WinformId + "')";
                        if (BaseFactory<Sys_Function>.Instance.ExecuteNonQuery(sql)>0)
                        {
                            XtraMessageBox.Show("保存成功！");
                            this.Close();
                        }
                    }
                    
                    
                    //}
                    //else
                    //{
                    //    Lamex.Model.Sys_FunctionInfo fun = BaseFactory<Sys_Function>.Instance.GetEntriyList(" and FunControlID='" + txtWinformId.Text + "'").FirstOrDefault();
                    //    function.PID = txtMenu.Value;
                    //    function.FunName = txtName.Text;
                    //    function.FunControlID = txtWinformId.Text;
                    //    function.SystemType = txtSystemType.Text;
                    //    function.SortCode = txtSortCode.Text;
                    //    function.CreateBy = "lzw";
                    //    function.CreateDate = DateTime.Now;
                        

                    //}

                    
                    
                }
                else
                {
                    XtraMessageBox.Show("保存失败！");
                }

            }
            else
            {
                info.PID = txtMenu.Value;
                info.Name = txtName.Text;
                info.WinformId = txtWinformId.Text;
                info.SortCode = txtSortCode.Text;
                info.WinformType = txtWinformType.Text;
                info.Image = txtIcon.Text;
                info.SystemType = txtSystemType.Text;
                info.Enabled = chkEnabled.Checked;
                if (txtMenu.Value == "-1")
                    info.MainMenu = txtWinformType.Text;
                info.CreateBy = Loginer.CurrentUser.Account;
                info.CreateTime = DateTime.Now;
                info.EngName = txtEngName.Text;
                if (BaseFactory<Sys_Menu>.Instance.Update(info))
                {
                    Sys_FunctionInfo function = BaseFactory<Sys_Function>.Instance.GetEntriyList(" 1=1 and ID='" + _ID + "'").FirstOrDefault();
                    if (function != null)
                    {
                        function.PID = txtMenu.Value;
                        function.FunName = txtName.Text;
                        function.FunControlID = txtWinformId.Text;
                        function.SystemType = txtSystemType.Text;
                        function.SortCode = txtSortCode.Text;
                        function.CreateBy = Loginer.CurrentUser.Account;
                        function.CreateDate = DateTime.Now;
                        function.SysMenu = txtWinformId.Text;
                        
                        if (BaseFactory<Sys_Function>.Instance.Update(function))
                        {
                            XtraMessageBox.Show("保存成功！");
                            this.Close();
                        }
                        //XtraMessageBox.Show("保存成功！");
                    }
                }
                else
                {
                    XtraMessageBox.Show("保存失败！");
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Clear();
            _ID = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void Clear()
        {
            txtName.Text="";
            txtMenu.Value="";
            txtEngName.Text = "";
            txtSortCode.Text="";
            txtWinformId.Text="";
            txtWinformType.Text="";
            txtIcon.Text="";
            chkEnabled.Checked=false;
            txtEngName.Text = "";
        }
    }
}
